<?php
/*
 * @Author: yukun-ABMO2O 3420326614@qq.com
 * @Date: 2023-09-30 21:45:35
 * @LastEditors: yukun-ABMO2O 3420326614@qq.com
 * @LastEditTime: 2023-10-04 19:41:02
 * @FilePath: \80\config\router.php
 */

class Router
{
    private $routes = [];

    // private $urlPrefix ="/api/v1";
    private $urlPrefix = null;
    // get请求
    public function get($url, $controller, $action, $middleware = false)
    {
        $this->addRoute('GET', $url, $controller, $action, $middleware);
    }
    // post请求
    public function post($url, $controller, $action, $middleware = false)
    {
        $this->addRoute('POST', $url, $controller, $action, $middleware);
    }
    // 以下不推荐使用
    // put请求
    public function put($url, $controller, $action, $middleware = false)
    {
        $this->addRoute('PUT', $url, $controller, $action, $middleware);
    }
    // delete请求
    public function delete($url, $controller, $action, $middleware = false)
    {
        $this->addRoute('DELETE', $url, $controller, $action, $middleware);
    }

    // 添加到路由数组
    private function addRoute($method, $url, $controller, $action, $middleware)
    {
        if ($this->urlPrefix) {
            $newurl = $this->urlPrefix . $url;
        } else {
            $newurl = $url;
        }
        $this->routes[$newurl] = [
            'method' => $method,
            'controller' => $controller,
            'action' => $action,
            'middleware' => $middleware,
        ];
    }
    // 设置路由前缀
    public function setUrl($url)
    {
        $this->urlPrefix = $url;
    }

    // 返回路由数组
    public function getRoutes()
    {
        return $this->routes;
    }
}
